"""工具函数模块 - 修复版本，展示正确的类型注解"""

import json
import os
from typing import Any, Dict, List, Optional, Union


def load_config(file_path: str) -> Optional[Dict[str, Any]]:
    """加载配置文件 - 添加了返回类型注解"""
    if not os.path.exists(file_path):
        print(f"配置文件不存在: {file_path}")
        return None

    with open(file_path, "r", encoding="utf-8") as f:
        data: Dict[str, Any] = json.load(f)
        return data


def process_data(data: Optional[Union[List[Any], Dict[str, Any], Any]]) -> Optional[Any]:
    """处理数据 - 添加了完整的类型注解"""
    if data is None:
        return None

    if isinstance(data, list):
        return [item * 2 for item in data if item > 0]
    elif isinstance(data, dict):
        result: Dict[str, Any] = {}
        for key, value in data.items():
            if isinstance(value, str):
                result[key] = value.upper()
            else:
                result[key] = value
        return result
    else:
        return str(data).upper()


def validate_email(email: str) -> bool:
    """验证邮箱格式 - 添加了类型注解"""
    import re

    pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
    return re.match(pattern, email) is not None


class DataProcessor:
    """数据处理器类 - 添加了完整的类型注解"""

    def __init__(self, config: Optional[Dict[str, Any]] = None) -> None:
        self.config = config or {}
        self.processed_count = 0

    def process_batch(self, items: List[Any]) -> List[Any]:
        """批处理数据"""
        results: List[Any] = []
        for item in items:
            try:
                processed = self._process_single(item)
                results.append(processed)
                self.processed_count += 1
            except Exception as e:
                print(f"处理项目时出错: {e}")  # 应该使用 logging
                continue

        return results

    def _process_single(self, item: Any) -> Any:
        """处理单个项目"""
        if isinstance(item, str):
            return item.strip().lower()
        elif isinstance(item, (int, float)):
            return item * 2
        elif isinstance(item, dict):
            return {k: str(v).upper() for k, v in item.items()}
        else:
            return str(item)

    def get_stats(self) -> Dict[str, int]:
        """获取处理统计信息 - 添加了返回类型注解"""
        return {"processed_count": self.processed_count}


# 修复后的函数
def properly_named_function() -> None:
    """函数名符合 Python 命名规范"""
    pass


def used_function(x: float, y: float) -> float:
    """使用所有参数的函数"""
    return x + y


def function_with_proper_line_length() -> str:
    """修复行长度问题的函数"""
    very_long_string = (
        "这是一个被正确分行的长字符串，"
        "符合88个字符的限制，"
        "展示了如何处理长字符串"
    )
    return very_long_string
